﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Jayden.Dll.Web.Crawler
{

	public interface UriValidator
	{
		bool IsValidUri(WebCrawlerUri uri);
	}

	public class NotUriValidator : UriValidator
	{
		private UriValidator m_InnerValidator;

		public NotUriValidator(UriValidator innerValidator)
		{
			m_InnerValidator = innerValidator;
		}

		public bool IsValidUri(WebCrawlerUri uri) { return !m_InnerValidator.IsValidUri(uri); }
	}
	public class AndUriValidator : List<UriValidator>, UriValidator
	{
		public bool IsValidUri(WebCrawlerUri uri)
		{
			foreach (UriValidator innerValidator in this)
				if (!innerValidator.IsValidUri(uri))
					return false;
			return true;
		}
	}
	public class OrUriValidator : List<UriValidator>, UriValidator
	{
		public bool IsValidUri(WebCrawlerUri uri)
		{
			foreach (UriValidator innerValidator in this)
				if (innerValidator.IsValidUri(uri))
					return true;
			return false;
		}
	}
	public class DepthUriValidator : UriValidator
	{
		private int m_MaximumDepth = 10;

		public DepthUriValidator(int maximumDepth = 10)
		{
			m_MaximumDepth = maximumDepth;
		}

		public bool IsValidUri(WebCrawlerUri uri)
		{
			if (uri.Depth > m_MaximumDepth)
				return false;
			return true;
		}
	}
	public class ExtensionUriValidator : UriValidator
	{
		private HashSet<string> m_AllowedExtensions = new HashSet<string>();

		public ExtensionUriValidator(params string[] allowedExtensions)
		{
			foreach (string allowedExtension in allowedExtensions)
				m_AllowedExtensions.Add(allowedExtension);
		}

		public bool IsValidUri(WebCrawlerUri uri)
		{
			string extension = Path.GetExtension(uri.Uri.AbsolutePath);
			return m_AllowedExtensions.Contains(extension);
		}
	}

}
